from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QTextEdit, QPushButton, QHBoxLayout
from PyQt5.QtCore import Qt

class ScheduleDetailDialog(QDialog):
    def __init__(self, schedule, parent=None):
        super().__init__(parent)
        self.schedule = schedule
        self.setWindowTitle("日程详情")
        icon = QIcon("E:/pythonwork/personal_schedule_system/rsc/icon.jpg")
        self.setWindowIcon(icon)
        self.resize(400, 300)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()

        # 显示日程详情
        layout.addWidget(QLabel(f"<b>标题：</b> {self.schedule.title}"))
        layout.addWidget(QLabel(f"<b>开始时间：</b> {self.schedule.start_time.strftime('%Y-%m-%d %H:%M')}"))

        if self.schedule.end_time:
            layout.addWidget(QLabel(f"<b>结束时间：</b> {self.schedule.end_time.strftime('%Y-%m-%d %H:%M')}"))
        else:
            layout.addWidget(QLabel("<b>结束时间：</b> 未设置"))

        if self.schedule.reminder_at:
            layout.addWidget(QLabel(f"<b>提醒时间：</b> {self.schedule.reminder_at.strftime('%Y-%m-%d %H:%M')}"))
        else:
            layout.addWidget(QLabel("<b>提醒时间：</b> 无"))

        notes = QTextEdit(self.schedule.notes or "（无备注）")
        notes.setReadOnly(True)
        layout.addWidget(QLabel("<b>备注：</b>"))
        layout.addWidget(notes)

        # 按钮区域
        btn_layout = QHBoxLayout()

        self.edit_btn = QPushButton("编辑")
        self.edit_btn.clicked.connect(self.open_edit_dialog)
        btn_layout.addWidget(self.edit_btn)
        self.edit_btn.setStyleSheet("background-color: #4a90e2; color: white;")

        close_btn = QPushButton("关闭")
        close_btn.clicked.connect(self.accept)
        btn_layout.addWidget(close_btn)
        close_btn.setStyleSheet("background-color: #f5f5f5; color: #666666;")

        layout.addLayout(btn_layout)
        self.setLayout(layout)

        self.setLayout(layout)

        self.setStyleSheet("""
                    /* 主窗口 */
                    QDialog {
                        background-color: qlineargradient(
                            spread:pad, x1:0, y1:0, x2:1, y2:1,
                            stop:0 #e0f7fa, stop:1 #ffffff
                        );
                        border-radius: 12px;
                        border: 1px solid rgba(255, 255, 255, 0.3);
                    }

                    /* 按钮样式 */
                    QPushButton {
                        font-family: 'Microsoft YaHei';
                        font-size: 8pt;
                        font-weight: medium;
                        border-radius: 8px;
                        padding: 0 25px;
                        min-width: 80px;
                        height: 40px;
                        border: none;
                    }
                """)

    def open_edit_dialog(self):
        """打开编辑对话框"""
        from ui.schedule_dialog import ScheduleDialog
        edit_dialog = ScheduleDialog(
            user_id=self.schedule.user_id,
            schedule=self.schedule
        )
        edit_dialog.exec_()

        #     # 编辑成功后刷新显示

        #     # 通知父窗口刷新列表
        #     if self.parent() and hasattr(self.parent(), 'load_schedules'):
        #         self.parent().load_schedules()

